<%@page pageEncoding="UTF-8" %>

<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Iterator"%>
<%@page import ="com.sxt.bbs.db.DB"%>
<%@page import = "com.sxt.bbs.entity.Article" %>
<%@page import = "java.sql.Connection" %>
<%@page import = "java.sql.Statement" %>
<%@page import = "java.sql.ResultSet" %>
<%@page import = "java.sql.SQLException" %>
<%@page import = "java.util.List" %>
<%@page import = "java.util.ArrayList" %>

<%@ include file="_session_check.jsp" %>

<%!
public void delete(Connection conn, int id, boolean isLeaf) {
	//delete all of the children
	
	if(!isLeaf) {
	
		String sql = "select * from article where pid = " + id;
		Statement stat = DB.createStat(conn);
		ResultSet rs = DB.excuteQuerey(stat, sql);
		try {
			while(rs.next()) {
				delete(conn, rs.getInt("id"), rs.getInt("isleaf") == 0);
			}
		} catch(SQLException e) {
			e.printStackTrace();
		} finally {
			DB.close(rs);
			DB.close(stat);
		}
	}
	
	
	DB.excuteUpdate(conn, "delete from article where id = " + id);
}
%>

<%
int id = Integer.parseInt(request.getParameter("id"));
int pid = Integer.parseInt(request.getParameter("pid"));
boolean isLeaf = Boolean.parseBoolean(request.getParameter("isleaf"));
String url = request.getParameter("from");

Connection conn = null;
Statement stat = null;
ResultSet rs = null;
boolean autoCommit = true;
try {

	conn = DB.getConn();
	autoCommit = conn.getAutoCommit();
	conn.setAutoCommit(false);
	
	delete(conn, id, isLeaf);
	
	stat = conn.createStatement();
	rs = DB.excuteQuerey(stat, "select count(*) from article where pid = " + pid);
	rs.next();
	int count = rs.getInt(1);
	if(count < 0) {
		DB.excuteUpdate(conn, "update article set isleaf = 0 where id = " + pid);
	}
	conn.commit();
} finally {
	conn.setAutoCommit(autoCommit);
	DB.close(rs);
	DB.close(stat);
	DB.close(conn);
	
}

response.sendRedirect("article.jsp");
%>
